package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.SetmealMapper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.dao.SetmealMapper;
import com.itheima.pojo.Setmeal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService{

    @Autowired
    public SetmealMapper setmealMapper;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Setmeal> pageSetmeal = setmealMapper.findByCondition(queryPageBean.getQueryString());
        return new PageResult(pageSetmeal.getTotal(), pageSetmeal.getResult());
    }

    @Override
    public Integer delById(Integer id) {
        long count = setmealMapper.findCountById(id);
        if(count>0){
            return -1;
        }
        setmealMapper.delRelation(id);
        setmealMapper.delById(id);
        return 1;
    }

    @Override
    public Setmeal findById(Integer id) {
       return setmealMapper.findById(id);
    }

    @Override
    public List<Integer> findCheckgroupIdsWithSetmeal(Integer id) {
        return setmealMapper.findCheckgroupIdsWithSetmeal(id);
    }
    @Override
    public Setmeal findDetailsById(Integer id) {
        return setmealMapper.findDetailsById(id);
    }

    @Override
    public List<Map> getSetmealReport() {
        return setmealMapper.getSetmealReport();
    }

 @Override
    public Integer update(Setmeal setmeal, Integer[] checkgroupId) {
        Integer id = setmeal.getId();
        if (id != null){
            //删除关联关系
            Integer delCheckGroupById = setmealMapper.delSetMealById(id);
            //添加新的关联关系

            //修改检查组
            if (setmeal != null){
                return setmealMapper.update(setmeal);
            }
            return null;
        }
        return null;
    }

    @Override
    public List<Integer> findCheckGroupIdsBySetmealId(Integer id) {
        List ids = setmealMapper.findCheckGroupIdsBySetmealId(id);
        if (ids.size() > 0){
            return ids;
        }
        return null;
    }

    @Override
    public List<Setmeal> findAll() {
        return setmealMapper.findAll();}
    @Override
    @Transactional
    public Integer add(Setmeal setmeal, List<Integer> checkGroupIds) {
        try {
            Integer count = setmealMapper.addSetmeal(setmeal);
            if (count != 1){
                return -1;
            }
            Integer setmealId = setmeal.getId();
            Integer integer = setmealMapper.addSetmealAndCheckGroup(setmealId, checkGroupIds);
            if (integer != 1){
                return -1;
            }
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }

    }
}
